<?php

namespace App\Http\Controllers;

use App\Channel;
use App\News;
use Illuminate\Http\Request;
use MercurySeries\Flashy\Flashy;

class SubpageController extends Controller
{
    public function show()
    {
        $channels = Channel::where('type' ,2)->get();
        return view('subpage.show' ,compact('channels'));
    }

    public function edit($channel_id)
    {
        $detail = News::where('channel_id' ,$channel_id)->first();
//        dump($detail);
        return view('subpage.edit' ,compact('detail','channel_id'));
    }

    public function update(Request $request)
    {
        $news = News::where('channel_id' ,$request->channel_id)->first();
        if ( is_null($news) ){
            $das = News::create([
                'channel_id'=>$request->channel_id,
                'content'=> $request->content,
                'is_post'=> 0,
            ]);
//            dd($das);
        }
        else {
            News::where('channel_id', $request->channel_id)->update([
                'content' => $request->content,
            ]);


        }
        Flashy::success('修改成功');
        return redirect()->back();
    }
}
